Ext.define('Admin.view.settle.TradeResultDetailListController', {
	        extend : 'Admin.app.CrudController',
	        alias : 'controller.tradeResultDetailListController',

	        onTradeDetailListRefresh : function(view, record, params) {
		        var me = this, vm = me.getViewModel();
		        var searchDate = Ext.getCmp('searchDate').getValue();
		        var date = Ext.util.Format.date(searchDate,'Y-m');
		        var store = vm.getStore('tradeResultDetailListStore');
		        //传递参数
		         store.proxy.extraParams = {
		         id : record.data.id,
		         level:record.data.level,
		         date:date
		         };
		        store.reload();
	        },

	        onParamsChange : function(cmp, newValue, oldValue, eOpts) {
				var me = this, vm = me.getViewModel(), view = me.getView(), record = vm.get('record');
				if (view.isVisible() && cmp.isVisible() && record) { // 判断isVisible()，防止页面未构建但是因为赋初值而误动作
					// debugger;
					var params = {};
					params[cmp.getBind().value.stub.name] = newValue;
					view.fireEvent('refresh', view, record, params);
				}
			},
			onPrevClick : function(btn) {
				var me = this, vm = me.getViewModel(), date = vm.get('date');
				vm.set('date', Ext.Date.add(date, Ext.Date.MONTH, -1));
			},

			onNextClick : function(btn) {
				var me = this, vm = me.getViewModel(), date = vm.get('date');
				vm.set('date', Ext.Date.add(date, Ext.Date.MONTH, 1));
			},
			
			onCellDblClick : function(view, td, cellIndex, record, tr, rowIndex, e, eOpts) {
		        var grid = view.ownerCt, headerCt = grid.getHeaderContainer();
		        var column = headerCt.getHeaderAtIndex(cellIndex);
		        if (0 == cellIndex || 'checkcolumn' === column.xtype) { // 所有的check框，都不能产生双击效果
			        return;
		        }
		        Ext.applyIf(record.data,{name:'编辑交易结果'});
		        this.showDetail(grid.detail, record);
	        },
	        onInsertClick : function(btn) {
		        var me = this, grid = btn.ownerCt.ownerCt, parent = me.getViewModel().get('record');
		        if (!parent)
			        return;
		        var searchDate = Ext.getCmp('searchDate').getValue();
		        var date = Ext.util.Format.date(searchDate,'Y-m');
		        var record = Ext.create(grid.getStore().model, {
			                id : null,
			                name : '录入交易结果',
			                market : parent.data.id,
			                data:date
		                });
		        record.store = grid.getStore();
		        
		        //先显示添加窗口
		        var win = me.showDetailAdd('tradeResultDetailListDetailAdd', record);
		        var detailList = win.items.items[0];
		        
		        Ext.Ajax.timeout=7200000;
	            Ext.Ajax.request({
	        		url:"rest/api/traderesultdetail/getContractPlantByCon?orgPlant="+parent.data.id+"&month="+date,
	        		method:"GET",
	        		success:function(response,options ){
	        			var result = response.responseText;
	        			var dataStart = result.indexOf("^");
	        			var dataEnd = result.indexOf("!");
	        			var data = result.substring(dataStart+1,dataEnd);
	        			var plans =[];
	        			if(data!=''){
	        			  plans = data.split('@');
	        			}
	        			if(plans.length>0){
	        				for(var i=0;i<plans.length;i++){
	        					var plan = plans[i];
	        					var planId = plan.split("_")[0];
	        					var planName = plan.split("_")[1];
	        					//生成formPanle-添加一行
	        					var tradeResultDetail_formPanel = new  Ext.form.FormPanel({           
	        		                //column布局控件开始  
	        		                layout: 'column',
	        		                border: false,
	        		                items:[{//计划
	        		                	 columnWidth: .3,
	        			                 layout: 'form',
	        			                 border: false,
	        			                 items:[{
	        			                	    xtype: 'xlongfield',
	        			                    	name:'planId',
	        			                    	hidden:true,
	        			                    	value:planId,
	        			                        triggerAction: 'all',
	        			                        anchor: '90%'
	        			                 },{
	        			                	    xtype: 'textfield',
	        			                    	name:'planName',
	        			                    	allowBlank: false,
	        			                    	readOnly:true,
	        			                    	fieldLabel: '计划',
	        			                        value:planName,
	        			                        triggerAction: 'all',
	        			                        anchor: '90%'
	        			                 }]
	        		                },{
	        		                	//电量
	        		                	 columnWidth: .2,
	        			                 layout: 'form',
	        			                 border: false,
	        			                 items:[{
	        			                	    xtype: 'xnumberfield',
	        			                    	name:'energy',
	        			                    	fieldLabel: '电量',
	        			                    	allowBlank: false,
	        			                        blankText: '电量不能为空',
	        			                        emptyText: '',
	        			                        triggerAction: 'all',
	        			                        anchor: '90%'
	        			                 }]
	        		                },{
	        		                	 columnWidth: .2,
	        			                 layout: 'form',
	        			                 border: false,
	        			                 items:[{
	        			                	    xtype: 'xnumberfield',
	        			                    	name:'price',
	        			                    	fieldLabel: '电价',
	        			                        blankText: '电价不能为空',
	        			                        emptyText: '',
	        			                        triggerAction: 'all',
	        			                        allowBlank: false,
	        			                        anchor: '90%'
	        			                 }]
	        		                },{
	        		                	 columnWidth: .2,
	        			                 layout: 'form',
	        			                 border: false,
	        			                 items:[{
	        			                	xtype: 'xnumberfield',
	 					                	name:'fee',
	 					                	fieldLabel: '电费',
	 					                    allowBlank: false,
	 					                    anchor: '90%'
	        			                 }]
	        		                },{
	        		                	columnWidth: .1,
	        		                    layout: 'form',
	        		                    border: false,
	        		                    algin:'right',
	        		                    items:[{
	        		                    	 	xtype: 'button',
	        			                        algin:'right',
	        			                        ui : 'soft-green',
	        									iconCls : 'x-fa fa-plus-square fa-lg',
	        			                        text: '添加峰平谷',
	        			                        value: id,
	        			                        handler: function(btn){
	        			                        	//添加峰平谷数据的录入
	        			                        	var peakFlatValley = new Ext.form.FormPanel({
	        			        		                layout: 'column',
	        			        		                width:1200,
	        			        		                border: false,
	        			        		                items: [{
	        			        		    	        	columnWidth: 0.28,
	        			        		    	        	padding:'0 0 0 0',
	        			        		    	        	items:[{
	        			        		    		        	xtype : 'xnumberfield',
	        			        		    		        	name:'peakEnergy',
	        			        		    	                fieldLabel : '峰段：电量',
	        			        		    	                labelWidth:120
	        			        		    	               },{
	        			        		    			        	xtype : 'xnumberfield',
	        			        		    			        	name:'flatEnergy',
	        			        		    		                fieldLabel : '平段：电量',
	        			        		    		                labelWidth:120
	        			        		    		               },{
	        			        		    				        	xtype : 'xnumberfield',
	        			        		    				        	name:'valleyEnergy',
	        			        		    			                fieldLabel : '谷段：电量',
	        			        		    			                labelWidth:120
	        			        		    			                }]
	        			        		    	        },{
	        			        		    	        	columnWidth: 0.25,  
	        			        		    	        	padding:'0 0 0 0',
	        			        		    	        	items:[{
	        			        		        	        	xtype : 'xnumberfield',
	        			        		        	        	name:'peakPrice',
	        			        		                        fieldLabel : '电价'
	        			        		                       // padding:'0 0 0 0',
	        			        		                       },{
	        			        		            	        	xtype : 'xnumberfield',
	        			        		            	        	name:'flatPrice',
	        			        		                            fieldLabel : '电价'
	        			        		                            },{
	        			        		                	        	xtype : 'xnumberfield',
	        			        		                	        	name:'valleyPrice',
	        			        		                                fieldLabel : '电价'
	        			        		                               }]
	        			        		    	        },{
	        			        		    	        	columnWidth: 0.25,  
	        			        		    	        	padding:'0 0 0 0',
	        			        		    	        	items:[{
	        			        		        	        	xtype : 'xnumberfield',
	        			        		        	        	name:'peakFee',
	        			        		                        fieldLabel : '电费'
	        			        		                        //readOnly:true,
	        			        		                       },{
	        			        		            	        	xtype : 'xnumberfield',
	        			        		            	        	name:'flatFee',
	        			        		                            fieldLabel : '电费'
	        			        		                            //readOnly:true,
	        			        		                           },{
	        			        		                	        	xtype : 'xnumberfield',
	        			        		                	        	name:'valleyFee',
	        			        		                                fieldLabel : '电费',
	        			        		                                columnWidth: 0.32
	        			        		                                // readOnly:true,
	        			        		                                }]   	       
	        			        		    	        	       
	        			        		    	        },{
	        			        		    	        	columnWidth: 0.1,
	        			        		                	xtype:'button',
	        			        		                	algin:'right',
	        			        		                	text: '删除峰平谷',
	        			        		                	ui : 'soft-red',
	        			        							iconCls : 'x-fa fa-trash-o fa-lg',
	        			        		                	//padding:'0 0 0 10',
	        			        		                	handler:function(btn){
	        			        		                		btn.ownerCt.ownerCt.remove(this.ownerCt, false);
	        			        		                	}
	        			        		    	        }]
	        			                        	});
	        			                        	
	        			                        	btn.ownerCt.ownerCt.add(peakFlatValley);
	        			                        }
	        		                    }]
	        		                }]
	        					});
	        					
	        					//
	        					detailList.add(tradeResultDetail_formPanel);
	        				}
	        			}
	        		}
	        	});
		        
	        },
	        showDetailAdd : function(detail, record) {
		        var me = this, record = me.duplicate(record);
		        var win = me.showWin(detail, record.getText(), me.onOkAdd);
		        win.getViewModel().set('record', record);
		        me.afterDetail(record, win);
		        win.show();
		        return win;
	        },
	        onOkAdd : function(button) {
	        	//刷新页面
	        	var controller = this;//当前controller
	        	var view =  this.getView();
	        	
	        	  //var items = button.ownerCt.ownerCt.items.items;
	        	  var items = button.ownerCt.ownerCt.items.items;
		          var length = items.length;
		          var json = '';
		          if(length>0){
		        	  for(var i=0;i<length;i++){
		        		  if(!items[i].isValid()) return;
			        	  var data = items[i].getValues();
			        	  var flatEnergy = (data.flatEnergy == undefined ?0:data.flatEnergy);
			        	  var flatPrice = data.flatPrice ==undefined ? 0 :data.flatPrice;
			        	  var flatFee = data.flatFee == undefined?0:data.flatFee;
			        	  var peakEnergy = data.peakEnergy == undefined?0:data.peakEnergy;
			        	  var peakFee = data.peakFee == undefined?0:data.peakFee;
			        	  var peakPrice = data.peakPrice == undefined?0:data.peakPrice;
			        	  var valleyEnergy = data.valleyEnergy == undefined?0:data.valleyEnergy;
			        	  var valleyFee = data.valleyFee == undefined?0:data.valleyFee;
			        	  var valleyPrice = data.valleyPrice == undefined?0:data.valleyPrice;
			        	  if(i==0){
		            			json+="^{\"disable\":\""+data.planId+"\",\"energy\":"+data.energy+",\"price\":"+data.price+",\"fee\":"+data.fee+",\"peakEnergy\":"+peakEnergy+",\"peakPrice\":"+peakPrice+",\"peakFee\":"+peakFee+",\"flatEnergy\":"+flatEnergy+",\"flatPrice\":"+flatPrice+",\"flatFee\":"+flatFee+",\"valleyEnergy\":"+valleyEnergy+",\"valleyPrice\":"+valleyPrice+",\"valleyFee\":"+valleyFee+"}";
		            		}else{
		            			json+=",{\"disable\":\""+data.planId+"\",\"energy\":"+data.energy+",\"price\":"+data.price+",\"fee\":"+data.fee+",\"peakEnergy\":"+peakEnergy+",\"peakPrice\":"+peakPrice+",\"peakFee\":"+peakFee+",\"flatEnergy\":"+flatEnergy+",\"flatPrice\":"+flatPrice+",\"flatFee\":"+flatFee+",\"valleyEnergy\":"+valleyEnergy+",\"valleyPrice\":"+valleyPrice+",\"valleyFee\":"+valleyFee+"}";
		            		}
			          }
		          }
		          json+="*";
		            Ext.Ajax.timeout=7200000;
		            Ext.Ajax.request({
		        		url:"rest/api/traderesultdetail/saveList/"+json,
		        		method:"PUT",
		    			headers:{
		                    'Content-Type': 'application/json; charset=utf-8'
		    					},
		        		success:function(response,options ){
		        			var win = button.ownerCt.ownerCt.ownerCt;
		    		        win.close();
		    		        //刷新结果列表
		    		        controller.refreshView(view);
		        		}
		        	});
	        },
	        afterDetail : function(record, win) {
		        var me = this, vm = me.getViewModel(), store = vm.getStore('programStore');
		        win.getViewModel().set('programStore', store);
	        },

	        onTradeResult : function(view, rowIndex, colIndex, item, e, record, row) {
	        	var trade = view.ownerCt.ownerCt.ownerCt;
	        	trade.getController().showTradeResult(record.data);
	        }
        });